package com.guigu.youxuan.controller;

import com.github.pagehelper.PageInfo;
import com.guigu.youxuan.pojo.OrderInfo;
import com.guigu.youxuan.pojo.PurchaseOrder;
import com.guigu.youxuan.service.IPurchaseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.guigu.youxuan.common.utils.PageUtils;
import com.guigu.youxuan.pojo.PurchaseOrder;
import com.guigu.youxuan.pojo.SupplierInfo;
import com.guigu.youxuan.pojo.dto.PurchaseAndDetailDto;
import com.guigu.youxuan.pojo.dto.PurchaseDto;
import com.guigu.youxuan.pojo.dto.PurchaseReviewDto;
import com.guigu.youxuan.pojo.dto.SupplierGoodsDto;
import com.guigu.youxuan.service.IPurchaseOrderService;
import com.guigu.youxuan.utils.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liweixin
 * @since 2024-05-15
 */
@CrossOrigin
@Controller
@RequestMapping("/purchaseOrder")
public class PurchaseOrderController {
    @Autowired
    private IPurchaseOrderService iPurchaseOrderService;
    @Autowired
    private NumberUtils numberUtils;
    /**
     * 供应商订单分页查询采购订单信息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @RequestMapping("/SupplierOrderListPage/{currentPage}/{pageSize}/{sysUsersId}")
    @ResponseBody
    public PageInfo<PurchaseOrder> SupplierOrderListPage(@PathVariable int sysUsersId,
                                                        @PathVariable int currentPage,
                                                        @PathVariable int pageSize
    ){
        PageInfo<PurchaseOrder> page=this.iPurchaseOrderService.SupplierOrderListPage(sysUsersId,currentPage,pageSize);
        return page;
    }

    /**
     * 供应商根据订单表id查询对应的订单信息
     * @param id
     * @return
     */
    @RequestMapping("/OrderInfoById/{id}")
    @ResponseBody
    public PurchaseOrder OrderDetailsById(@PathVariable int id){
        PurchaseOrder goodsInfo=this.iPurchaseOrderService.getById(id);
        return goodsInfo;
    }

    /**
     * 供应商订单信息 点击发货后配送标志改为 P001-4 待收货
     * @param orderInfo
     * @return
     */
    @RequestMapping("/OrderInfoupdateId")
    @ResponseBody
    public String OrderInfoupdateId(@RequestBody PurchaseOrder orderInfo){
        orderInfo.setConveyTag("P001-3");
        System.out.println("发货"+orderInfo.getConveyTag()+"发货");
        System.out.println("id"+orderInfo.getPurchaseId()+"id");
        boolean flag=this.iPurchaseOrderService.updateById(orderInfo);
        if(flag){
            return "ok";
        }
        return "error";
    }



    /**
     * 复核通过的采购单查询
     * @param supplierInfo
     * @return
     */
    @RequestMapping("/queryPurchaseReviewOk/{currentPage}/{pageSize}")
    @ResponseBody
    public PageUtils<PurchaseReviewDto> queryPurchaseReviewOk(@RequestBody SupplierInfo supplierInfo,
                                                              @PathVariable int currentPage,
                                                              @PathVariable int pageSize) {
        PageUtils<PurchaseReviewDto> page = iPurchaseOrderService.queryPurchaseReviewOk(supplierInfo,currentPage,pageSize);
        return page;
    }

    /**
     * 采购审核通过
     * @param purchaseOrder
     * @return
     */
    @RequestMapping("/purchaseReviewOk")
    @ResponseBody
    public String purchaseReviewOk(@RequestBody PurchaseOrder purchaseOrder) {
        iPurchaseOrderService.purchaseReviewOk(purchaseOrder);
        return "采购审核通过！";
    }

    /**
     * 采购单和采购明细查询
     * @param purchaseId
     * @return
     */
    @RequestMapping("/queryPurchaseAndDetail/{purchaseId}")
    @ResponseBody
    public PurchaseAndDetailDto queryPurchaseAndDetail(@PathVariable String purchaseId) {
        PurchaseAndDetailDto dto = iPurchaseOrderService.queryPurchaseAndDetail(purchaseId);
        return dto;
    }


    /**
     * 采购单复核查询
     * @param supplierInfo
     * @return
     */
    @RequestMapping("/queryPurchaseReview/{currentPage}/{pageSize}")
    @ResponseBody
    public PageUtils<PurchaseReviewDto> queryPurchaseReview(@RequestBody SupplierInfo supplierInfo,
                                                            @PathVariable int currentPage,
                                                            @PathVariable int pageSize) {
        PageUtils<PurchaseReviewDto> page = iPurchaseOrderService.queryPurchaseReview(supplierInfo,currentPage,pageSize);
        return page;
    }

    /**
     * 生成采购单
     * @param purchaseDto
     * @return
     */
    @RequestMapping("/createPurchase")
    @ResponseBody
    public String createPurchase(@RequestBody PurchaseDto purchaseDto) {
        int flag =0;
        String register = purchaseDto.getRegister();
        List<SupplierGoodsDto> list = purchaseDto.getSupplierGoodsDtoList();
        Map<String,List<SupplierGoodsDto>> map = new HashMap();
        for(SupplierGoodsDto dto : list) {
            String supplierId = dto.getSupplierId();
            // 如果map中还没有这个supplierId的列表，则创建一个新的列表
            map.putIfAbsent(supplierId, new ArrayList<>());
            // 将dto添加到对应的列表中
            map.get(supplierId).add(dto);
        }
        for (Map.Entry<String, List<SupplierGoodsDto>> entry : map.entrySet()) {
            // 获取键（supplierId）
            String supplierId = entry.getKey();

            // 获取与键关联的SupplierGoodsDto对象列表
            List<SupplierGoodsDto> supplierGoodsList = entry.getValue();

            PurchaseOrder purchaseOrder = new PurchaseOrder();
            String purchaseOrderNum = numberUtils.getTypeNumber("100");
            purchaseOrder.setPurchaseOrderNumber(purchaseOrderNum);
            purchaseOrder.setSupplierId(Integer.parseInt(supplierId));
            purchaseOrder.setRegister(register);
            purchaseOrder.setTotalAmount(0);
            purchaseOrder.setTotalPrice(0);

            // 遍历列表中的每个SupplierGoodsDto对象
            for (SupplierGoodsDto supplierGoodsDto : supplierGoodsList) {
                purchaseOrder.setTotalAmount(supplierGoodsDto.getAmount()+purchaseOrder.getTotalAmount());
                purchaseOrder.setTotalPrice(supplierGoodsDto.getPrice()*supplierGoodsDto.getAmount()+purchaseOrder.getTotalPrice());
            }

            iPurchaseOrderService.createPurchase(purchaseOrder,supplierGoodsList);
            flag++;
        }

        return "已生成"+flag+"条出库单，请前往审核！";
    }
}
